﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model;
using Gift.DAL;

namespace WebSite.cn
{
    public partial class account : BasePage
    {
        // Fields
        public string address = "";
        public string carid = "0";
        public StringBuilder carinfo = new StringBuilder();
        public string cname = "";
        public string email = "";

        public string phone = "";
        public string post = "";
        public string score1 = "0";
        public string score2 = "0";
        public string score3 = "0";
        // public string shipmsg = "";
        public string tel = "";
        public string totalprice = "0";
        public string totalscore = "0";
        public string accountmoney = "0";
        public string sendDate = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");

        public string isHaveCard = "0";
        public string isHaveProfession = "0";
        public string isHaveShop = "0";
        public string tempOrderCode = "";

        public string UserCardTotalPrice = "";

        public string VisibleLogin = "none";

        readonly MemberAccount theMemberAccount = new MemberAccount();
        readonly Member theMember = new Member();
        readonly OrderAccount theOrderAccount = new OrderAccount();

        public string TempType
        {
            get
            {

                if (base.UserCardCodeList != null)
                {
                    return "1";
                }
                else if (base.UserMemberId != null)
                {
                    return "2";
                }
                else
                {
                    return Request.QueryString["type"];
                }
            }
        }

        public string SendDate
        {
            get
            {
                return DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");
            }
        }

        public string BackHref
        {
            get
            {
                if (Session["backHref"] != null)
                {
                    return Session["backHref"].ToString() + "&isView=1";
                }
                else
                {
                    return "#";
                }
            }
        }

        public string ServerSendDate
        {
            get
            {
                return DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");
            }
        }
        public string ToDate
        {
            get
            {
                return DateTime.Now.AddDays(5).ToString("yyyy-MM-dd");
            }
        }

        // Methods
        private void Bind()
        {
            DataTable table;
            if (base.UserMemberId != null)
            {
                VisibleLogin = "none";
                table = SQLAccess.GreatDs("select membername,tel1,phone,post,address,email,score1,score2,score3 from DB_Member where memberid=" + this.Session["memberid"] + "").Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.cname = table.Rows[0]["membername"].ToString();
                    this.tel = table.Rows[0]["tel1"].ToString();
                    this.phone = table.Rows[0]["phone"].ToString();
                    this.post = table.Rows[0]["post"].ToString();
                    this.address = table.Rows[0]["address"].ToString();
                    this.email = table.Rows[0]["email"].ToString();
                    this.score1 = table.Rows[0]["score1"].ToString();
                    this.score2 = table.Rows[0]["score2"].ToString();
                    this.score3 = table.Rows[0]["score3"].ToString();
                    //  this.sendDate = table.Rows[0]["sendDate"].ToString();
                }
            }
            else
            {
                VisibleLogin = "block";
            }
            if (base.Request.QueryString["action"] != null)
            {
                string str3 = re.replace(base.Request.QueryString["carid"]);
                if (re.replace(base.Request.QueryString["action"]) == "edit")
                {
                    string str2 = re.replace(base.Request.Form["car_" + str3 + ""]);
                    table = SQLAccess.GreatDs("select top 1 carid,pronum,price,score from DB_Car where carid=" + str3 + "").Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        SQLAccess.DoSql("update DB_Car set pronum=" + str2 + ",sumprice=" + Convert.ToString((float)(Convert.ToSingle(str2) * Convert.ToSingle(table.Rows[0]["price"]))) + ",sumscore=" + Convert.ToString((float)(Convert.ToSingle(str2) * Convert.ToSingle(table.Rows[0]["score"]))) + " where carid=" + table.Rows[0]["carid"].ToString() + "");
                        base.Response.Write("<script language='javascript'>alert('修改成功!');window.location.href='account.aspx';</script>");
                        base.Response.End();
                    }
                }
                if (re.replace(base.Request.QueryString["action"]) == "del")
                {
                    SQLAccess.DoSql("delete from DB_Car where carid=" + str3 + "");
                    base.Response.Write("<script language='javascript'>alert('删除成功!');window.location.href='account.aspx';</script>");
                    base.Response.End();
                }
            }
            if ((re.ReadCookie("tempno") == null) && base.TempOrderId == null)
            {
                this.carinfo.Append("<tr><td align='center' colspan='6' height=45>您的购物车中无礼品</td></tr>");
            }
            else
            {

                table = SQLAccess.GreatDs("select DB_Car.carid,DB_Car.pronum,DB_Car.price,DB_Car.sumprice,DB_Car.sumscore,DB_Car.contents,DB_Shoppro.typeid,DB_Shoppro.proname,DB_Shoppro.proprice1,DB_Shoppro.propic2,DB_Shoppro.proid"
                                           + ",isnull(DB_Shoppro.proisnew4,0) as proisnew4,isnull(DB_Shoppro.proisnew2,0) as proisnew2,isnull(DB_Shoppro.proisnew3,0) as proisnew3  from DB_Car inner join DB_Shoppro on DB_Car.productid=DB_Shoppro.proid and DB_Shoppro.visible=1 and isnull(DB_Shoppro.proku1,0) >0 where DB_Car.flag=0 and DB_Car.temp=" + base.TempOrderId + "").Tables[0];
                if (table.Rows.Count > 0)
                {

                    foreach (DataRow row in table.Rows)
                    {
                        if (isHaveCard == "0")
                        {
                            if (bool.Parse(row["proisnew3"].ToString()) == true)
                            {
                                isHaveCard = "1";
                            }
                        }
                        if (isHaveProfession == "0")
                        {
                            if (bool.Parse(row["proisnew2"].ToString()) == true)
                            {
                                isHaveProfession = "1";
                            }
                        }
                        if (isHaveShop == "0")
                        {
                            if (bool.Parse(row["proisnew4"].ToString()) == true)
                            {
                                isHaveShop = "1";
                            }
                        }
                        this.carid = this.carid + "," + row["carid"].ToString();
                        this.totalprice = string.Format("{0:f}", Convert.ToSingle(row["sumprice"]) + Convert.ToSingle(this.totalprice));
                        this.totalscore = Convert.ToString((uint)(Convert.ToUInt32(row["sumscore"]) + Convert.ToUInt32(this.totalscore)));
                        this.carinfo.Append("<tr>");
                        this.carinfo.Append( "<td align='center'><a href='products_details.aspx?pid=" + row["proid"].ToString() + "'><img src='../uploadfile/product/" + row["propic2"].ToString() + "' align='left' border='0'  width='106' /></a></td>");
                        this.carinfo.Append("<td align='left' class='article_content'><a href='products_details.aspx?pid=" + row["proid"].ToString() + "' class='blue'>" + row["proname"].ToString() + "</a><br />" + row["contents"].ToString() + "</td>");
                        this.carinfo.Append( "<td align='center'>" + string.Format("{0:f}", row["price"]) + " 元 </td>");
                        this.carinfo.Append( "<td align='center'>" +  row["pronum"].ToString() + "</td>");
                        this.carinfo.Append( "<td align='center'>" + string.Format("{0:f}", row["sumprice"]) + " 元 </td>");
                        this.carinfo.Append("</tr>");
                    }

                    

                    this.tempOrderCode = theOrderAccount.GenerateOrderCodeForBefor();
                }
                else
                {
                    this.carinfo.Append("<tr><td align='center' colspan='6' height=45>您的购物车中无礼品</td></tr>");
                }
            }

        }




        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                try
                {
                    if ((re.ReadCookie("tempno") != null) && base.TempOrderId == null)
                    {
                        base.TempOrderId = re.ReadCookie("tempno");
                    }

                    this.Bind();
                    this.GetAccountMoney();
                    if (UserCardCodeList != null)
                    {

                        decimal dTotalCardPrice = 0;
                        for (int i = 0; i < UserCardCodeList.Count; i++)
                        {
                            dTotalCardPrice += decimal.Parse(UserCardCodeList[i].UserCardPrice);
                        }
                        UserCardTotalPrice = dTotalCardPrice.ToString();

                    }
                }
                catch (Exception ex)
                {
                    LogFile.Write(ELogType.Error, ex.Message);
                  //  LogFile.SendEmail(ex.Message);
                }
               
            }
        }

        private void GetAccountMoney()
        {

            if (base.UserMemberId == null)
            {
                accountmoney = "0";
            }
            else
            {
                //decimal dMomey = 0;
                //ds = theMemberAccount.GetListByMemberId(int.Parse(Session["memberid"].ToString()));
                //if (ds != null && ds.Tables.Count > 0)
                //{
                //    foreach (DataRow dr in ds.Tables[0].Rows)
                //    {
                //        dMomey =  dMomey + decimal.Parse( dr["cardprice"].ToString());
                //    }
                //}
                //accountmoney = int.Parse(dMomey.ToString()).ToString();

                //DB_Member oMember;
                //oMember = theMember.GetModel(int.Parse(base.UserMemberId));
                //if (oMember != null)
                //{
                //    accountmoney = oMember.money.ToString();
                //}
                accountmoney = theMemberAccount.GetRemainSumMoney(int.Parse(base.UserMemberId)).ToString();
            }
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            base.UserCardCodeList = null;
            Response.Redirect("index.aspx");
        }



    }
}